package com.youlu.myapplication.scratch;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.FocusFinder;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.Scroller;

import java.util.ArrayList;

/**
 * @author sym
 */
public class BothWayScrollView extends FrameLayout {

    private static String TAG = "BothWayScrollView";

    private boolean DEBUG = true;

    private long a;
    private final Rect rect;
    private Scroller c;
    private boolean d;
    private boolean e;
    private float f;
    private float g;
    private int h;
    private boolean i;
    private View j;
    private VelocityTracker k;
    private boolean l;
    private boolean m;
    private boolean n;
    private int o;
    private int p;
    private int q;
    private int r;
    private boolean s;
    public a t;

    private int c(int i2, int i3, int i4) {
        if (i3 >= i4 || i2 < 0) {
            return 0;
        }
        return i3 + i2 > i4 ? i4 - i3 : i2;
    }

    public BothWayScrollView(Context context) {
        this(context, null);
    }

    public BothWayScrollView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.rect = new Rect();
        this.e = false;
        this.f = -1.0f;
        this.g = -1.0f;
        this.i = true;
        this.j = null;
        this.m = true;
        this.n = true;
        this.r = -1;
        this.s = false;
        a();
    }

    @Override
    public float getTopFadingEdgeStrength() {
        if (getChildCount() == 0) {
            return 0.0f;
        }
        int verticalFadingEdgeLength = getVerticalFadingEdgeLength();
        if (getScrollY() < verticalFadingEdgeLength) {
            return ((float) getScrollY()) / ((float) verticalFadingEdgeLength);
        }
        return 1.0f;
    }

    @Override
    public float getLeftFadingEdgeStrength() {
        if (getChildCount() == 0) {
            return 0.0f;
        }
        int horizontalFadingEdgeLength = getHorizontalFadingEdgeLength();
        if (getScrollX() < horizontalFadingEdgeLength) {
            return ((float) getScrollX()) / ((float) horizontalFadingEdgeLength);
        }
        return 1.0f;
    }

    @Override
    public float getRightFadingEdgeStrength() {
        if (getChildCount() == 0) {
            return 0.0f;
        }
        int horizontalFadingEdgeLength = getHorizontalFadingEdgeLength();
        int right = (getChildAt(0).getRight() - getScrollX()) - (getWidth() - getPaddingRight());
        if (right < horizontalFadingEdgeLength) {
            return ((float) right) / ((float) horizontalFadingEdgeLength);
        }
        return 1.0f;
    }

    @Override
    public float getBottomFadingEdgeStrength() {
        if (getChildCount() == 0) {
            return 0.0f;
        }
        int verticalFadingEdgeLength = getVerticalFadingEdgeLength();
        int bottom = (getChildAt(0).getBottom() - getScrollY()) - (getHeight() - getPaddingBottom());
        if (bottom < verticalFadingEdgeLength) {
            return ((float) bottom) / ((float) verticalFadingEdgeLength);
        }
        return 1.0f;
    }

    public int getMaxScrollAmountV() {
        return (int) (((float) (getBottom() - getTop())) * 0.5f);
    }

    public int getMaxScrollAmountH() {
        return (int) (((float) (getRight() - getLeft())) * 0.5f);
    }

    private void a() {
        this.c = new Scroller(getContext());
        setFocusable(true);
        setDescendantFocusability(262144);
        setWillNotDraw(false);
        ViewConfiguration viewConfiguration = ViewConfiguration.get(getContext());
        this.o = viewConfiguration.getScaledTouchSlop();
        this.p = viewConfiguration.getScaledMinimumFlingVelocity();
        this.q = viewConfiguration.getScaledMaximumFlingVelocity();
    }

    @Override
    public void addView(View view) {
        if (getChildCount() <= 0) {
            super.addView(view);
            return;
        }
        throw new IllegalStateException("ScrollView can host only one direct child");
    }

    @Override
    public void addView(View view, int i2) {
        if (getChildCount() <= 0) {
            super.addView(view, i2);
            return;
        }
        throw new IllegalStateException("ScrollView can host only one direct child");
    }

    @Override
    public void addView(View view, ViewGroup.LayoutParams layoutParams) {
        if (getChildCount() <= 0) {
            super.addView(view, layoutParams);
            return;
        }
        throw new IllegalStateException("ScrollView can host only one direct child");
    }

    @Override
    public void addView(View view, int i2, ViewGroup.LayoutParams layoutParams) {
        if (getChildCount() <= 0) {
            super.addView(view, i2, layoutParams);
            return;
        }
        throw new IllegalStateException("ScrollView can host only one direct child");
    }

    private boolean b() {
        View childAt = getChildAt(0);
        if (childAt == null) {
            return false;
        }
        if (getHeight() < childAt.getHeight() + getPaddingTop() + getPaddingBottom()) {
            return true;
        }
        return false;
    }

    private boolean c() {
        View childAt = getChildAt(0);
        if (childAt == null) {
            return false;
        }
        if (getWidth() < childAt.getWidth() + getPaddingLeft() + getPaddingRight()) {
            return true;
        }
        return false;
    }

    public void setFillViewport(boolean z) {
        if (z != this.l) {
            this.l = z;
            requestLayout();
        }
    }

    public void setSmoothScrollingEnabled(boolean z) {
        this.m = z;
    }

    @Override
    public void onMeasure(int i2, int i3) {
        super.onMeasure(i2, i3);
        if (this.l) {
            int mode = View.MeasureSpec.getMode(i3);
            int mode2 = View.MeasureSpec.getMode(i2);
            if (!(mode == 0 && mode2 == 0) && getChildCount() > 0) {
                View childAt = getChildAt(0);
                int measuredHeight = getMeasuredHeight();
                int measuredWidth = getMeasuredWidth();
                if (childAt.getMeasuredHeight() < measuredHeight || childAt.getMeasuredWidth() < measuredWidth) {
                    childAt.measure(View.MeasureSpec.makeMeasureSpec((measuredWidth - getPaddingLeft()) - getPaddingRight(), MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec((measuredHeight - getPaddingTop()) - getPaddingBottom(), MeasureSpec.EXACTLY));
                }
            }
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
        return super.dispatchKeyEvent(keyEvent) || a(keyEvent);
    }

    public boolean a(KeyEvent keyEvent) {
        this.rect.setEmpty();
        if (keyEvent.getAction() == MotionEvent.ACTION_DOWN) {
            switch (keyEvent.getKeyCode()) {
                case 19:
                    if (b()) {
                        if (!keyEvent.isAltPressed()) {
                            return c(33);
                        }
                        return a(33);
                    }
                    break;
                case 20:
                    if (b()) {
                        if (!keyEvent.isAltPressed()) {
                            return c(130);
                        }
                        return a(130);
                    }
                    break;
                case 21:
                    if (c()) {
                        if (!keyEvent.isAltPressed()) {
                            return d(17);
                        }
                        return b(17);
                    }
                    break;
                case 22:
                    if (c()) {
                        if (!keyEvent.isAltPressed()) {
                            return d(66);
                        }
                        return b(66);
                    }
                    break;
            }
        }
        return false;
    }

    private boolean c(int i2, int i3) {
        if (getChildCount() <= 0) {
            return false;
        }
        int scrollX = getScrollX();
        int scrollY = getScrollY();
        View childAt = getChildAt(0);
        if (i3 < childAt.getTop() - scrollY || i3 >= childAt.getBottom() - scrollY || i2 < childAt.getLeft() - scrollX || i2 >= childAt.getRight() - scrollX) {
            return false;
        }
        return true;
    }

    @Override
    public void requestDisallowInterceptTouchEvent(boolean z) {
        this.n = z;
        if (!z) {
            this.e = false;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        if (this.n) {
            return false;
        }
        int action = motionEvent.getAction();
        if (action == MotionEvent.ACTION_MOVE) {
            return true;
        }
        int i2 = action & MotionEvent.ACTION_MASK;
        if (i2 != 0) {
            if (i2 != 1) {
                if (i2 == 2) {
                    int i3 = this.r;
                    if (i3 != -1) {
                        int findPointerIndex = motionEvent.findPointerIndex(i3);
                        float y = motionEvent.getY(findPointerIndex);
                        if (((int) Math.abs(y - this.f)) > this.o) {
                            this.f = y;
                        }
                        float x = motionEvent.getX(findPointerIndex);
                        if (((int) Math.abs(x - this.g)) > this.o) {
                            this.g = x;
                        }
                    }
                } else if (i2 != 3) {
                    if (i2 == 6) {
                        a(motionEvent);
                    }
                }
            }
            this.r = -1;
        } else {
            float x2 = motionEvent.getX();
            float y2 = motionEvent.getY();
            if (c((int) x2, (int) y2)) {
                this.f = y2;
                this.g = x2;
                this.r = motionEvent.getPointerId(0);
            }
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN && motionEvent.getEdgeFlags() != 0) {
            return false;
        }
        if (this.k == null) {
            this.k = VelocityTracker.obtain();
        }
        this.k.addMovement(motionEvent);
        int action = motionEvent.getAction() & MotionEvent.ACTION_MASK;
        if (action != MotionEvent.ACTION_DOWN) {
            if (action == MotionEvent.ACTION_UP) {
                if (this.s) {
                    VelocityTracker velocityTracker = this.k;
                    velocityTracker.computeCurrentVelocity(1000, (float) this.q);
                    int xVelocity = (int) velocityTracker.getXVelocity(this.r);
                    int yVelocity = (int) velocityTracker.getYVelocity(this.r);
                    if (getChildCount() > 0 && (Math.abs(xVelocity) > xVelocity || Math.abs(yVelocity) > this.p)) {
                        b(-xVelocity, -yVelocity);
                    }
                }
                this.r = -1;
                VelocityTracker velocityTracker2 = this.k;
                if (velocityTracker2 != null) {
                    velocityTracker2.recycle();
                    this.k = null;
                }
                this.g = -1.0f;
                this.f = -1.0f;
            } else if (action != MotionEvent.ACTION_MOVE) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    if (action == MotionEvent.ACTION_POINTER_UP) {
                        this.e = true;
                        a(motionEvent);
                    }
                } else if (getChildCount() > 0) {
                    this.r = -1;
                    VelocityTracker velocityTracker3 = this.k;
                    if (velocityTracker3 != null) {
                        velocityTracker3.recycle();
                        this.k = null;
                    }
                }
            } else if (!this.e) {
                if (this.g >= 0.0f || this.f >= 0.0f) {
                    float y = motionEvent.getY();
                    int i1 = (int) (this.f - y);
                    this.f = y;
                    float x = motionEvent.getX();
                    int i2 = (int) (this.g - x);
                    this.g = x;
                    a(i2, i1);
                } else {
                    this.g = motionEvent.getX();
                    this.f = motionEvent.getY();
                }
            }
        }
        return true;
    }

    private void a(MotionEvent motionEvent) {
        int action = (motionEvent.getAction() & 65280) >> 8;
        if (motionEvent.getPointerId(action) == this.r) {
            int i2 = action == 0 ? 1 : 0;
            this.g = motionEvent.getX(i2);
            this.f = motionEvent.getY(i2);
            this.r = motionEvent.getPointerId(i2);
            VelocityTracker velocityTracker = this.k;
            if (velocityTracker != null) {
                velocityTracker.clear();
            }
        }
    }

    private View a(boolean z, int i2, int i3) {
        //2
        ArrayList focusables = getFocusables(FOCUS_FORWARD);
        int size = focusables.size();
        View view = null;
        boolean z2 = false;
        for (int i4 = 0; i4 < size; i4++) {
            View view2 = (View) focusables.get(i4);
            int top = view2.getTop();
            int bottom = view2.getBottom();
            if (i2 < bottom && top < i3) {
                boolean z3 = i2 < top && bottom < i3;
                if (view == null) {
                    view = view2;
                    z2 = z3;
                } else {
                    boolean z4 = (z && top < view.getTop()) || (!z && bottom > view.getBottom());
                    if (z2) {
                        if (z3) {
                            if (!z4) {
                            }
                        }
                    } else if (z3) {
                        view = view2;
                        z2 = true;
                    } else if (!z4) {
                    }
                    view = view2;
                }
            }
        }
        return view;
    }

    private View b(boolean z, int i2, int i3) {
        //2
        ArrayList focusables = getFocusables(FOCUS_FORWARD);
        int size = focusables.size();
        View view = null;
        boolean z2 = false;
        for (int i4 = 0; i4 < size; i4++) {
            View view2 = (View) focusables.get(i4);
            int left = view2.getLeft();
            int right = view2.getRight();
            if (i2 < right && left < i3) {
                boolean z3 = i2 < left && right < i3;
                if (view == null) {
                    view = view2;
                    z2 = z3;
                } else {
                    boolean z4 = (z && left < view.getLeft()) || (!z && right > view.getRight());
                    if (z2) {
                        if (z3) {
                            if (!z4) {
                            }
                        }
                    } else if (z3) {
                        view = view2;
                        z2 = true;
                    } else if (!z4) {
                    }
                    view = view2;
                }
            }
        }
        return view;
    }

    public boolean a(int i2) {
        int childCount;
        boolean z = i2 == 130;
        int height = getHeight();
        Rect rect = this.rect;
        rect.top = 0;
        rect.bottom = height;
        if (z && (childCount = getChildCount()) > 0) {
            this.rect.bottom = getChildAt(childCount - 1).getBottom();
            Rect rect2 = this.rect;
            rect2.top = rect2.bottom - height;
        }
        return a(i2, this.rect.top, this.rect.bottom);
    }

    public boolean b(int i2) {
        boolean z = i2 == 66;
        int width = getWidth();
        Rect rect = this.rect;
        rect.left = 0;
        rect.right = width;
        if (z && getChildCount() > 0) {
            this.rect.right = getChildAt(0).getRight();
            Rect rect2 = this.rect;
            rect2.left = rect2.right - width;
        }
        return b(i2, this.rect.left, this.rect.right);
    }

    private boolean a(int i2, int i3, int i4) {
        boolean z;
        int height = getHeight();
        int scrollY = getScrollY();
        int i5 = height + scrollY;
        boolean z2 = i2 == 33;
        View a2 = a(z2, i3, i4);
        if (a2 == null) {
            a2 = this;
        }
        if (i3 < scrollY || i4 > i5) {
            e(z2 ? i3 - scrollY : i4 - i5);
            z = true;
        } else {
            z = false;
        }
        if (a2 != findFocus() && a2.requestFocus(i2)) {
            this.d = true;
            this.d = false;
        }
        return z;
    }

    private boolean b(int i2, int i3, int i4) {
        boolean z;
        int width = getWidth();
        int scrollX = getScrollX();
        int i5 = width + scrollX;
        boolean z2 = i2 == 17;
        View b2 = b(z2, i3, i4);
        if (b2 == null) {
            b2 = this;
        }
        if (i3 < scrollX || i4 > i5) {
            f(z2 ? i3 - scrollX : i4 - i5);
            z = true;
        } else {
            z = false;
        }
        if (b2 != findFocus() && b2.requestFocus(i2)) {
            this.d = true;
            this.d = false;
        }
        return z;
    }

    public boolean c(int i2) {
        int bottom;
        View findFocus = findFocus();
        if (findFocus == this) {
            findFocus = null;
        }
        View findNextFocus = FocusFinder.getInstance().findNextFocus(this, findFocus, i2);
        int maxScrollAmountV = getMaxScrollAmountV();
        if (findNextFocus == null || !a(findNextFocus, maxScrollAmountV, getHeight())) {
            if (i2 == 33 && getScrollY() < maxScrollAmountV) {
                maxScrollAmountV = getScrollY();
            } else if (i2 == 130 && getChildCount() > 0 && (bottom = getChildAt(0).getBottom() - (getScrollY() + getHeight())) < maxScrollAmountV) {
                maxScrollAmountV = bottom;
            }
            if (maxScrollAmountV == 0) {
                return false;
            }
            if (i2 != 130) {
                maxScrollAmountV = -maxScrollAmountV;
            }
            e(maxScrollAmountV);
        } else {
            findNextFocus.getDrawingRect(this.rect);
            offsetDescendantRectToMyCoords(findNextFocus, this.rect);
            e(a(this.rect));
            findNextFocus.requestFocus(i2);
        }
        if (findFocus == null || !findFocus.isFocused() || !a(findFocus)) {
            return true;
        }
        int descendantFocusability = getDescendantFocusability();
        setDescendantFocusability(131072);
        requestFocus();
        setDescendantFocusability(descendantFocusability);
        return true;
    }

    public boolean d(int i2) {
        int right;
        View findFocus = findFocus();
        if (findFocus == this) {
            findFocus = null;
        }
        View findNextFocus = FocusFinder.getInstance().findNextFocus(this, findFocus, i2);
        int maxScrollAmountH = getMaxScrollAmountH();
        if (findNextFocus == null || !a(findNextFocus, maxScrollAmountH)) {
            if (i2 == 17 && getScrollX() < maxScrollAmountH) {
                maxScrollAmountH = getScrollX();
            } else if (i2 == 66 && getChildCount() > 0 && (right = getChildAt(0).getRight() - (getScrollX() + getWidth())) < maxScrollAmountH) {
                maxScrollAmountH = right;
            }
            if (maxScrollAmountH == 0) {
                return false;
            }
            if (i2 != 66) {
                maxScrollAmountH = -maxScrollAmountH;
            }
            f(maxScrollAmountH);
        } else {
            findNextFocus.getDrawingRect(this.rect);
            offsetDescendantRectToMyCoords(findNextFocus, this.rect);
            f(b(this.rect));
            findNextFocus.requestFocus(i2);
        }
        if (findFocus == null || !findFocus.isFocused() || !b(findFocus)) {
            return true;
        }
        int descendantFocusability = getDescendantFocusability();
        setDescendantFocusability(131072);
        requestFocus();
        setDescendantFocusability(descendantFocusability);
        return true;
    }

    private boolean a(View view) {
        return !a(view, 0, getHeight());
    }

    private boolean b(View view) {
        return !a(view, 0);
    }

    private boolean a(View view, int i2, int i3) {
        view.getDrawingRect(this.rect);
        offsetDescendantRectToMyCoords(view, this.rect);
        return this.rect.bottom + i2 >= getScrollY() && this.rect.top - i2 <= getScrollY() + i3;
    }

    private boolean a(View view, int i2) {
        view.getDrawingRect(this.rect);
        offsetDescendantRectToMyCoords(view, this.rect);
        return this.rect.right + i2 >= getScrollX() && this.rect.left - i2 <= getScrollX() + getWidth();
    }

    private void e(int i2) {
        if (i2 == 0) {
            return;
        }
        if (this.m) {
            a(0, i2);
        } else {
            scrollBy(0, i2);
        }
    }

    private void f(int i2) {
        if (i2 == 0) {
            return;
        }
        if (this.m) {
            a(i2, 0);
        } else {
            scrollBy(i2, 0);
        }
    }

    public void a(int i2, int i3) {
        if (getChildCount() != 0) {
            boolean z = false;
            while (true) {
                int i4 = this.h;
                int i5 = this.o;
                if (i4 <= i5) {
                    break;
                }
                z = true;
                this.h = i4 - i5;
            }
            if (z) {
                this.t.a();
            }
            if (AnimationUtils.currentAnimationTimeMillis() - this.a > 250) {
                int max = Math.max(0, getChildAt(0).getHeight() - ((getHeight() - getPaddingBottom()) - getPaddingTop()));
                int scrollY = getScrollY();
                int max2 = Math.max(0, getChildAt(0).getWidth() - ((getWidth() - getPaddingRight()) - getPaddingLeft()));
                int scrollX = getScrollX();
                int maxX = Math.max(0, Math.min(i2 + scrollX, max2)) - scrollX;
                int maxY = Math.max(0, Math.min(i3 + scrollY, max)) - scrollY;
                this.h = 0;
                this.c.startScroll(scrollX, scrollY, maxX, maxY);
                invalidate();
            } else {
                if (!this.c.isFinished()) {
                    this.c.abortAnimation();
                }
                scrollBy(i2, i3);
                this.h += (int) Math.sqrt((double) ((i2 * i2) + (i3 * i3)));
            }
            this.a = AnimationUtils.currentAnimationTimeMillis();
        }
    }

    /* access modifiers changed from: protected */
    @Override
    public int computeVerticalScrollRange() {
        int childCount = getChildCount();
        int height = (getHeight() - getPaddingBottom()) - getPaddingTop();
        if (childCount == 0) {
            return height;
        }
        return getChildAt(0).getBottom();
    }

    /* access modifiers changed from: protected */
    @Override
    public int computeHorizontalScrollRange() {
        int childCount = getChildCount();
        int width = (getWidth() - getPaddingLeft()) - getPaddingRight();
        if (childCount == 0) {
            return width;
        }
        return getChildAt(0).getRight();
    }

    /* access modifiers changed from: protected */
    @Override
    public int computeVerticalScrollOffset() {
        return Math.max(0, super.computeVerticalScrollOffset());
    }

    /* access modifiers changed from: protected */
    @Override
    public int computeHorizontalScrollOffset() {
        return Math.max(0, super.computeHorizontalScrollOffset());
    }

    /* access modifiers changed from: protected */
    @Override
    public void measureChild(View view, int i2, int i3) {
        //0
        view.measure(View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    /* access modifiers changed from: protected */
    @Override
    public void measureChildWithMargins(View view, int i2, int i3, int i4, int i5) {
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        view.measure(View.MeasureSpec.makeMeasureSpec(marginLayoutParams.leftMargin + marginLayoutParams.rightMargin, MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(marginLayoutParams.topMargin + marginLayoutParams.bottomMargin, MeasureSpec.UNSPECIFIED));
    }

    @Override
    public void computeScroll() {
        if (this.c.computeScrollOffset()) {
            int currX = this.c.getCurrX();
            int currY = this.c.getCurrY();

            if (DEBUG) {
                Log.i(TAG, "currX : " + currX + " currY : " + currY);
            }

            if (getChildCount() > 0) {
                View childAt = getChildAt(0);
                int x = c(currX, (getWidth() - getPaddingRight()) - getPaddingLeft(), childAt.getWidth());
                int y = c(currY, (getHeight() - getPaddingBottom()) - getPaddingTop(), childAt.getHeight());
                super.scrollTo(x, y);
            }
            awakenScrollBars();
            postInvalidate();
        }
    }

    private void c(View view) {
        view.getDrawingRect(this.rect);
        offsetDescendantRectToMyCoords(view, this.rect);
        int a2 = a(this.rect);
        int b2 = b(this.rect);
        if (b2 != 0 || a2 != 0) {
            scrollBy(b2, a2);
        }
    }

    private boolean a(Rect rect, boolean z) {
        int a2 = a(rect);
        int b2 = b(rect);
        boolean z2 = (b2 == 0 && a2 == 0) ? false : true;
        if (z2) {
            if (z) {
                scrollBy(b2, a2);
            } else {
                a(b2, a2);
            }
        }
        return z2;
    }

    /* access modifiers changed from: protected */
    public int a(Rect rect) {
        int i2;
        int i3;
        if (getChildCount() == 0) {
            return 0;
        }
        int height = getHeight();
        int scrollY = getScrollY();
        int i4 = scrollY + height;
        int verticalFadingEdgeLength = getVerticalFadingEdgeLength();
        if (rect.top > 0) {
            scrollY += verticalFadingEdgeLength;
        }
        if (rect.bottom < getChildAt(0).getHeight()) {
            i4 -= verticalFadingEdgeLength;
        }
        if (rect.bottom > i4 && rect.top > scrollY) {
            if (rect.height() > height) {
                i3 = rect.top - scrollY;
            } else {
                i3 = rect.bottom - i4;
            }
            return Math.min(i3 + 0, getChildAt(0).getBottom() - i4);
        } else if (rect.top >= scrollY || rect.bottom >= i4) {
            return 0;
        } else {
            if (rect.height() > height) {
                i2 = 0 - (i4 - rect.bottom);
            } else {
                i2 = 0 - (scrollY - rect.top);
            }
            return Math.max(i2, -getScrollY());
        }
    }

    /* access modifiers changed from: protected */
    public int b(Rect rect) {
        int i2;
        int i3;
        if (getChildCount() == 0) {
            return 0;
        }
        int width = getWidth();
        int scrollX = getScrollX();
        int i4 = scrollX + width;
        int horizontalFadingEdgeLength = getHorizontalFadingEdgeLength();
        if (rect.left > 0) {
            scrollX += horizontalFadingEdgeLength;
        }
        if (rect.right < getChildAt(0).getWidth()) {
            i4 -= horizontalFadingEdgeLength;
        }
        if (rect.right > i4 && rect.left > scrollX) {
            if (rect.width() > width) {
                i3 = rect.left - scrollX;
            } else {
                i3 = rect.right - i4;
            }
            return Math.min(i3 + 0, getChildAt(0).getRight() - i4);
        } else if (rect.left >= scrollX || rect.right >= i4) {
            return 0;
        } else {
            if (rect.width() > width) {
                i2 = 0 - (i4 - rect.right);
            } else {
                i2 = 0 - (scrollX - rect.left);
            }
            return Math.max(i2, -getScrollX());
        }
    }

    @Override
    public void requestChildFocus(View view, View view2) {
        if (!this.d) {
            if (!this.i) {
                c(view2);
            } else {
                this.j = view2;
            }
        }
        super.requestChildFocus(view, view2);
    }

    /* access modifiers changed from: protected */
    @Override
    public boolean onRequestFocusInDescendants(int i2, Rect rect) {
        View view;
        if (rect == null) {
            view = FocusFinder.getInstance().findNextFocus(this, (View) null, i2);
        } else {
            view = FocusFinder.getInstance().findNextFocusFromRect(this, rect, i2);
        }
        if (view == null) {
            return false;
        }
        return view.requestFocus(i2, rect);
    }

    @Override
    public boolean requestChildRectangleOnScreen(View view, Rect rect, boolean z) {
        rect.offset(view.getLeft() - view.getScrollX(), view.getTop() - view.getScrollY());
        return a(rect, z);
    }

    @Override
    public void requestLayout() {
        this.i = true;
        super.requestLayout();
    }

    /* access modifiers changed from: protected */
    @Override
    public void onLayout(boolean z, int i2, int i3, int i4, int i5) {
        super.onLayout(z, i2, i3, i4, i5);
        this.i = false;
        View view = this.j;
        if (view != null && a(view, (View) this)) {
            c(this.j);
        }
        this.j = null;
        scrollTo(getScrollX(), getScrollY());
    }

    /* access modifiers changed from: protected */
    @Override
    public void onSizeChanged(int i2, int i3, int i4, int i5) {
        super.onSizeChanged(i2, i3, i4, i5);
        View findFocus = findFocus();
        if (findFocus != null && this != findFocus) {
            if (a(findFocus, 0, i5)) {
                findFocus.getDrawingRect(this.rect);
                offsetDescendantRectToMyCoords(findFocus, this.rect);
                e(a(this.rect));
            }
            if (a(findFocus, getRight() - getLeft())) {
                findFocus.getDrawingRect(this.rect);
                offsetDescendantRectToMyCoords(findFocus, this.rect);
                f(b(this.rect));
            }
        }
    }

    private boolean a(View view, View view2) {
        if (view == view2) {
            return true;
        }
        ViewParent parent = view.getParent();
        if (!(parent instanceof ViewGroup) || !a((View) parent, view2)) {
            return false;
        }
        return true;
    }

    public void b(int i2, int i3) {
        if (getChildCount() > 0) {
            int width = getChildAt(0).getWidth();
            int height = (getHeight() - getPaddingBottom()) - getPaddingTop();
            int i4 = i2;
            int i5 = i3;
            this.c.fling(getScrollX(), getScrollY(), i4, i5, 0, Math.max(0, width - ((getWidth() - getPaddingRight()) - getPaddingLeft())), 0, Math.max(0, getChildAt(0).getHeight() - height));
            invalidate();
        }
    }

    @Override
    public void scrollTo(int i2, int i3) {
        if (getChildCount() > 0) {
            View childAt = getChildAt(0);
            int c2 = c(i2, (getWidth() - getPaddingRight()) - getPaddingLeft(), childAt.getWidth());
            int c3 = c(i3, (getHeight() - getPaddingBottom()) - getPaddingTop(), childAt.getHeight());
            if (c2 != getScrollX() || c3 != getScrollY()) {
                Log.i(TAG, "width :: " + c2 + "  height :: " + c3);
                super.scrollTo(c2, c3);
            }
        }
    }

    public void setFlingEnabled(boolean z) {
        this.s = z;
    }

    public static abstract class a {
        public abstract void a();

        public void a(BothWayScrollView bothwayScrollView) {
            bothwayScrollView.t = this;
        }
    }
}